#include "MeshManager.h"

#include "File.h"
#include "ReportError.h"
#include <assert.h>

MeshManager::MeshManager()
{

}

bool MeshManager::Load(const std::string& fileName)
{
	File f;

	//Format is
	//file path
	//mesh name
	if(!f.OpenRead(fileName))
	{
		ReportError("MeshPaths can't open");
	}
	std::string str;
	while(f.GetString(&str))
	{
		ObjMesh* m = new ObjMesh;
		m->Load(str);
		m_meshes.insert(std::pair<std::string, ObjMesh*>(str, m));
	}
	return true;
}

void MeshManager::DrawMesh(const std::string& meshName)
{
	assert(m_meshes.size());
	m_it = m_meshes.find(meshName);
	if(m_it != m_meshes.end())
	{
		m_it->second->Draw();
	}
	else
	{
		ReportError("Cannot find mesh to draw");
		assert(0);
	}
}

ObjMesh* MeshManager::GetMesh(const std::string& meshName)
{
	m_it = m_meshes.find(meshName);
	if(m_it != m_meshes.end())
	{
		return m_meshes.find(meshName)->second;
	}

	else
	{
		ReportError("Mesh not found in mesh manager");
		return false;
	}
}